package hu.diploma.web.client.panels;

import hu.diploma.web.client.Messages;
import hu.diploma.web.client.WidgetFactory;
import hu.diploma.web.client.pages.Login;
import hu.diploma.web.client.pages.OpenForm;

import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.custom.ThemeSelector;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Image;

public class Header extends ContentPanel {
	private static Header instanse;
	private final Image image = new Image("contact.jpg");
	private final Messages messages = GWT.create(Messages.class);

	private Header() {

		setHeaderVisible(false);
		setFrame(true);
		// initWidget();

	}

	public void initWidget() {
		setLayout(new RowLayout(Orientation.HORIZONTAL));
		image.setSize("90px", "100px");
		Button login = new Button(messages.login());
		Button logout = new Button(messages.logout());

		ThemeSelector selector = new ThemeSelector();
		selector.setWidth(125);

		add(image);

		RowLayout ver = new RowLayout(Orientation.VERTICAL);

		add(selector);
		add(login);
		login.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				WidgetFactory.getCenterPanel().setWidget(Login.getWidget());

			}

		});

		logout.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				WidgetFactory.getCenterPanel().setWidget(OpenForm.getWidget());

				Login.setActUser(null);

				WidgetFactory.getMenu().initWidget();
				WidgetFactory.getHeader().initWidget();
			}

		});

		if (Login.getActUser() == null) {
			// add(login);
		} else {
			Text text = new Text(messages.hello() + Login.getActUser().getName());

			add(text);
			add(logout);
		}
	}

	public static Header getWidget() {
		if (instanse == null) {
			instanse = new Header();
		}
		return instanse;
	}
}
